10261 - Ferry loading (DP, programación dinámica) && 410 - Station balance (Dijkstra...
[and.git] / 10520 - Determine it / 10520.cpp
blob747980adc6833f8930e45c0cacdb25c4c023abe4
1 #include <stdio.h>
2 #include <algorithm>
4 using namespace std;
6 #define MAXN 19
8 int n, base;
9 long long cache[MAXN+1][MAXN+1];
11 long long a(const int &i, const int &j){
12 if (i == n && j == 0) return 0;
13 if (i == n && j == 1) return base;
15 if (cache[i][j] == -1){
16 long long &r = cache[i][j] = 0;
18 if (i < j){
19 for (int k=i; k<j; ++k){
20 r = max(r, a(i, k) + a(k+1, j));
22 }else{ //i >= j
23 long long x = 0, y = 0;
24 if (i < n){
25 for (int k=i+1; k<=n; ++k){
26 x = max(x, a(k, 1) + a(k, j));
30 if (j > 0){
31 for (int k=1; k<j; ++k){
32 y = max(y, a(i, k) + a(n, k));
35 r = x + y;
38 return cache[i][j];
41 int main(){
42 while (scanf("%d %d", &n, &base) == 2){
43 memset(cache, -1, sizeof cache);
44 printf("%lld\n", a(1, n));
46 return 0;